#include "NoSpread.h"

NoSpread g_pNoSpread;

void NoSpread::Init()
{
	Active = true;
	NoRecoil = true;
	NoSpreadMethod = true;
}

void NoSpread::ApplyNoRecoil( CUserCmd* cmd, bool dual /*= false*/ )
{
	if (Active && NoRecoil)
	{
		static SD_Tools::Vector LastRecoil(0, 0, 0);
		Entity* pMe = g_pInterfaces.g_pEntList->GetClientEntity(g_pInterfaces.g_pEngine->GetLocalPlayer());

		if (!pMe)
		{
			return;
		}

		if (pMe->iTeamNum() == TeamSpec)
		{
			return;
		}

		SD_Tools::Vector* punch = pMe->getRecoilVec();

		if (dual)
		{
			JUNK_CODE;
			cmd->viewangles.Pitch -= punch->x;
			cmd->viewangles.Yaw -= punch->y;
			GameUtilities::FixAngle(&cmd->viewangles);
			JUNK_CODE;
			if(g_pAimbot.Silent)
			{
				SD_Tools::Angle Temp;
				g_pInterfaces.g_pEngine->GetViewAngles(&Temp);
				Temp.x -= punch->x - LastRecoil.x;
				Temp.y -= punch->y - LastRecoil.y;
				JUNK_CODE;
				g_pInterfaces.g_pEngine->SetViewAngles(Temp);
				LastRecoil = *punch;
			}
			else
			{
				g_pInterfaces.g_pEngine->SetViewAngles(cmd->viewangles);
			}
			cmd->viewangles.Pitch -= punch->x * 0.92f;
			JUNK_CODE;
			cmd->viewangles.Yaw -= punch->y * 0.92f;
			GameUtilities::FixAngle(&cmd->viewangles);
		}
		else
		{
			SD_Tools::Angle Temp;

			g_pInterfaces.g_pEngine->GetViewAngles(&Temp);
			Temp.x -= punch->x - LastRecoil.x;
			JUNK_CODE;
			Temp.y -= punch->y - LastRecoil.y;
			GameUtilities::FixAngle(&Temp);
			g_pInterfaces.g_pEngine->SetViewAngles(Temp);
			cmd->viewangles.Pitch = Temp.x - punch->x * 0.92f;
			JUNK_CODE;
			cmd->viewangles.Yaw = Temp.y - punch->y * 0.92f;
			GameUtilities::FixAngle(&cmd->viewangles);

			LastRecoil = *punch;
		}
		cmd->viewangles.Roll = 0.0f;
	}
}

void NoSpread::Enable()
{
	Active = true;
}

void NoSpread::Disable()
{
	Active = false;
}

void NoSpread::Set( bool Active )
{
	this->Active = Active;
}

void NoSpread::SetMethod( bool Method )
{
	NoSpreadMethod = Method;
}

void NoSpread::SetRecoil( bool Active )
{
	NoRecoil = Active;
}

void NoSpread::ApplySeedSpread( CUserCmd* cmd )
{
	if (Active && NoSpreadMethod)
	{
		cmd->random_seed = 165;
		JUNK_CODE;
		cmd->command_number = 2087;
	}
}
